import { ref, Ref } from "vue"
import { PaginationParams, QueryParams } from "@purchasing/components"

import api from "@/api"

export interface ExportColumn {}

/**
 * 执行导出过程返回的结果
 *
 * @property isExporting 是否正在导出的响应式结果
 * @property executeExport 执行导出的过程
 */
export interface ExportReturnType {
  isExporting: Ref<boolean>
  executeExport: () => any[]
}

// 创建导出过程
export function useExport(
  url: string,
  params: QueryParams,
  columns: ExportColumn[],
): ExportReturnType {
  const isExporting = ref(false)

  const pagination: PaginationParams = { page: 1, size: 10 }

  const { data, error, execute } = api.useExportData(url, params, pagination)

  function executeExport() {
    isExporting.value = true
    execute()
      .then(() => {
        data.value && console.log("fetch returns data", data.value)
        error.value && console.log("fetch returns error", error.value)
        console.log("export xlsx with columns", columns)
      })
      .finally(() => {
        isExporting.value = false
      })
    return []
  }

  return {
    isExporting,
    executeExport,
  }
}
